package com.tjcd.project.library_base.common.widget.touch;

import android.annotation.SuppressLint;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static com.tjcd.project.library_base.common.widget.touch.TouchType.ANIM_ALPHA;
import static com.tjcd.project.library_base.common.widget.touch.TouchType.ANIM_SCALE;


/**
 * @Author jiangKunKun
 * @CreateDate 2021/7/9
 * @Description 下按动画
 * @SpittingGrooves
 */
public class Touch {
    /**
     * 单例
     */
    private static volatile Touch mTouch;

    @NonNull
    public static Touch getInstance() {
        if (mTouch == null) {
            synchronized (Touch.class) {
                if (mTouch == null) {
                    mTouch = new Touch();
                }
            }
        }
        return mTouch;
    }

    public static TouchBuilder with(View v) {
        return getInstance().getBuilder(v);
    }

    private TouchBuilder getBuilder(View v) {
        return new TouchBuilder(v);
    }

    /**
     * 构造器
     */
    public class TouchBuilder {
        /**
         * 抬起回调
         */
        private TouchLisenter mTouchUp;
        /**
         * 按下回调
         */
        private TouchLisenter mTouchDown;
        /**
         * 取消回调
         */
        private TouchLisenter mTouchCancel;
        /**
         * 动画类型
         */
        private int mAnimType;
        /**
         * 幅度
         */
        private float mExtent = 0.9f;
        /**
         * 时长
         */
        private long mDuration = 100;
        /**
         * 是否全部动画
         */
        private boolean mAnimAll = false;
        /**
         * 是否禁用
         */
        private boolean mEnable = true;
        /**
         * 根布局
         */
        private View mRootView;
        /**
         * 需要效果的views
         */
        private List<View> mViews;

        public TouchBuilder(View view) {
            mRootView = view;
        }

        public TouchBuilder anim(@TouchType.AnimType int anim) {
            mAnimType = anim;
            return this;
        }

        public TouchBuilder animAll(boolean all) {
            mAnimAll = all;
            return this;
        }

        public TouchBuilder extent(float extent) {
            mExtent = extent;
            return this;
        }

        public TouchBuilder enable(boolean enable) {
            mEnable = enable;
            return this;
        }

        public TouchBuilder duration(long duration) {
            mDuration = duration;
            return this;
        }

        public TouchBuilder touchUp(TouchLisenter touchUp) {
            mTouchUp = touchUp;
            return this;
        }

        public TouchBuilder touchDown(TouchLisenter touchDown) {
            mTouchDown = touchDown;
            return this;
        }

        public TouchBuilder touchCancel(TouchLisenter touchCancel) {
            mTouchCancel = touchCancel;
            return this;
        }

        public TouchBuilder views(View... views) {
            mViews = new ArrayList<>();
            mViews.addAll(Arrays.asList(views));
            return this;
        }

        public void touch() {
            if (null == mRootView) {
                Log.e("Touch", "未绑定View,请先绑定View");
                return;
            }
            if (!mEnable) {
                doTouch(mRootView, mViews, mAnimAll, mAnimType, mExtent, mDuration, true);
                return;
            }
            mRootView.setOnTouchListener(new View.OnTouchListener() {
                @SuppressLint("ClickableViewAccessibility")
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {
                    switch (motionEvent.getAction()) {
                        case MotionEvent.ACTION_DOWN:
                            doTouch(mRootView, mViews, mAnimAll, mAnimType, mExtent, mDuration, true);
                            if (null != mTouchDown) {
                                mTouchDown.onTouch(view, motionEvent);
                            }
                            break;
                        case MotionEvent.ACTION_CANCEL:
                            doTouch(mRootView, mViews, mAnimAll, mAnimType, 1, mDuration, false);
                            if (null != mTouchCancel) {
                                mTouchCancel.onTouch(view, motionEvent);
                            }
                            break;
                        case MotionEvent.ACTION_UP:
                            doTouch(mRootView, mViews, mAnimAll, mAnimType, 1, mDuration, false);
                            if (null != mTouchUp) {
                                mTouchUp.onTouch(view, motionEvent);
                            }
                            break;
                    }
                    return true;
                }
            });
        }

        private void doTouch(View view, List<View> views, boolean animAll, int animType, float extent, long duration, boolean selected) {
            /**
             * 动画
             */
            if (!animAll) {
                if (animType == ANIM_SCALE) {
                    view.animate().scaleX(extent).scaleY(extent).setDuration(duration).start();
                } else if (animType == ANIM_ALPHA) {
                    view.animate().alpha(extent).setDuration(duration).start();
                } else {
                    view.setSelected(selected);
                    view.setPressed(selected);
                }
            }
            /**
             * 普通切换状态
             */
            if (!ObjectUtils.isEmpty(views)) {
                for (int i = 0; i < views.size(); i++) {
                    views.get(i).setSelected(selected);
                    views.get(i).setPressed(selected);
                    if (animAll) {
                        if (animType == ANIM_SCALE) {
                            views.get(i).animate().scaleX(extent).scaleY(duration).setDuration(duration).start();
                        } else if (animType == ANIM_ALPHA) {
                            views.get(i).animate().alpha(extent).setDuration(duration).start();
                        }
                    }
                }
            }
        }
    }
}
